var express = require('express');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var sign = require('./sign.js');
var app = express();
var log = require('log');
var ejs = require('ejs');
var querystring = require('querystring');

app.use(cookieParser());
app.use(session({
    secret: '12345',
    name: 'sid',
    cookie: { maxAge: 80000, httpOnly: false, secure: false },
}));
app.post('/wxJssdk/getJssdk', (req, res) => {
    var data = "";
    var dataObject = "";
    req.on('data', function (chunk) {
        data += chunk;
    });
    req.on('end', function () {
        data = decodeURI(data);
        console.log(data);
        dataObject = querystring.parse(data);
        res.send(sign(dataObject.ticket,dataObject.url));
    });
})
var config = require("./config");
var webverbsion = config.webverbsion
// app.use(express.static(__dirname + '/dist' + webverbsion));
app.use(express.static(__dirname + '/public' + webverbsion));
app.engine('.html', ejs.__express);
app.set('view engine', 'html');
// app.set('views', __dirname + '/dist' + webverbsion);
app.set('views', __dirname + '/public' + webverbsion);
app.use(function (req, res) {
    console.log(req.path);
    if (/\.css$|\.gif$|\.html$|\.ico$|\.jpeg$|\.jpg$|\.js$|\.json$|\.pdf$|\.png$|\.svg$|\.swf$|\.tiff$|\.txt$|\.wav$|\.wma$|\.wmv$|\.xml$/i.test(req.path)) {
        res.status(404).send('not find');
    } else {
        res.render("html/index", { version: config.cacheverbsion });
    }
});
app.use(function (err, req, res, next) {
    log.info(req.url);
    log.info(err.stack);
    res.status(500).send('Something broke!');
});

var server = app.listen(config.port, function () {
    console.log('Listening on port %d', server.address().port);
});